package cn.com.zetatech.maple.core.local;

import cn.com.zetatech.maple.entity.FtpDFile;
import cn.com.zetatech.maple.local.LocalHandler;
import cn.com.zetatech.maple.parser.utils.ScriptEnginePool;
import lombok.Getter;
import lombok.Setter;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import java.io.IOException;

/**
 * 基于脚本的本地预处理
 */
@Setter
@Getter
public class LocalScriptHandler implements LocalHandler {
    private String script;
    private String engineName = "groovy";
    @Override
    public void handle(FtpDFile dFile) throws IOException {
        if(engineName == null || engineName.trim().isEmpty()){
            this.engineName = "groovy";
        }
        ScriptEngine engine = ScriptEnginePool.getEngine(this.engineName.toLowerCase());
        Bindings bindings = engine.createBindings();
        bindings.put("dFile", dFile);
        try {
            engine.eval(script, bindings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
